<?php
namespace app\admin\controller;
use app\common\model\StoreLink as StoreLinkModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
/**
 * 管理
 * Class Adminstorelink
 * @package app\admin\controller
 */
class StoreLink extends AdminBase
{
    protected $storelink_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->storelink_model = new StoreLinkModel();
    }
    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map['name|phone|site'] = ['like', "%{$keyword}%"];
        }
        $storelink_list = $this->storelink_model->where($map)->order('sort,id DESC')->paginate(15, false, ['page' => $page]);
        return $this->fetch('index', ['storelink_list' => $storelink_list, 'keyword' => $keyword]);
    }
    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 保存
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'StoreLink');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->storelink_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $storelink = $this->storelink_model->find($id);
        return $this->fetch('edit', ['storelink' => $storelink]);
    }
    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'StoreLink');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->storelink_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
    /**
     * 删除
     * @param $id
     */
    public function delete($id = '')
    {
        if ($this->storelink_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function update_status($id = '', $status = '')
    {
        $data['status'] = $status ? 0 : 1;
        if ($this->storelink_model->allowField(true)->where('id',$id)->update($data) !== false) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }
}